// FunkyDog MailBundle - tags and keywords for Apple Mail 3.0
//    Copyright (C) 2008 saptarshi guha saptarshi.guha@gmail.com
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#import "FunkyDog.h"
#import "TagPaneViewOwner.h"
#import <objc/objc.h>
#import <objc/objc-runtime.h>
#import <objc/objc-class.h>
NSString *SGTagsSet=@"SGuhaTagsSet";
NSString *TAGID=@"BodyTAG";
NSString *NOTEID=@"BodyNOTE";
NSString *STEROIDID=@"BodySTEROIDS";
int NOTETAG=17;
int STEROIDTAG=32;
int TAGTAG=16;

@implementation FunkySob
+ (void) initialize
{
    [super initialize];
    [self registerBundle]; //to call methods from here do : [ [self sharedInstance] methodname]
}
-(NSString*)fixMDQueryString:(NSString*)k{
	NSMutableString *s=[NSMutableString stringWithCapacity:[k length]];
	[s setString:k];
	[s replaceOccurrencesOfString:@"kmditemkeywords" withString:@"kMDItemKeywords" options:NSLiteralSearch
							range:NSMakeRange(0, [s length])];
	[s replaceOccurrencesOfString:@"kmditemcomment" withString:@"kMDItemComment" options:NSLiteralSearch
							range:NSMakeRange(0, [s length])];
	[s replaceOccurrencesOfString:@"kmditemauthoremailaddresses" withString:@"kMDItemAuthorEmailAddresses" options:NSLiteralSearch
							range:NSMakeRange(0, [s length])];
	[s replaceOccurrencesOfString:@"kmditemauthors" withString:@"kMDItemAuthors" options:NSLiteralSearch
							range:NSMakeRange(0, [s length])];
	[s replaceOccurrencesOfString:@"kmditemrecipientemailaddresses" withString:@"kMDItemRecipientEmailAddresses" options:NSLiteralSearch
							range:NSMakeRange(0, [s length])];
	[s replaceOccurrencesOfString:@"kmditemrecipients" withString:@"kMDItemRecipients" options:NSLiteralSearch
							range:NSMakeRange(0, [s length])];
	[s replaceOccurrencesOfString:@"kmditemsubject" withString:@"kMDItemSubject" options:NSLiteralSearch
							range:NSMakeRange(0, [s length])];
	[s replaceOccurrencesOfString:@"kmditemauthors" withString:@"kMDItemAuthors" options:NSLiteralSearch
							range:NSMakeRange(0, [s length])];
	[s replaceOccurrencesOfString:@"kmditemstarrating" withString:@"kMDItemStarRating" options:NSLiteralSearch
							range:NSMakeRange(0, [s length])];
	return s;
}
-(NSMutableDictionary*)cachesystem{return cachesystem;};

-(void)setSteroidSearch:(BOOL)k{ issteroidsearch=k;}
-(BOOL)issteroidsearch{return issteroidsearch;}
-(void)setTagSearch:(BOOL)k{ istagsearch=k;}
-(BOOL)istagsearch{return istagsearch;}
-(void)setNoteSearch:(BOOL)k{ isnotesearch=k;}
-(BOOL)isnotesearch{return isnotesearch;}
-(id)init{
	self=[super init];
	if(self){
		tags=[[NSMutableSet alloc] init];
		NSBundle	*myBundle = [NSBundle bundleForClass:[self class]];
        NSDictionary *defaultsDictionary = [NSDictionary dictionaryWithContentsOfFile:
											[myBundle pathForResource:@"FunkySob" ofType:@"defaults"]];
		cachesystem=[[NSMutableDictionary alloc] init];
		if(defaultsDictionary){
			mydefaults = [NSUserDefaults standardUserDefaults];
//			[mydefaults registerDefaults:defaultsDictionary];
			NSDictionary *d_=[mydefaults persistentDomainForName:[myBundle bundleIdentifier]];
			
			//http://lists.apple.com/archives/cocoa-dev/2006/Feb/msg00144.html
			[tags addObjectsFromArray:(NSArray*)[d_ objectForKey:SGTagsSet]];
//			NSLog(@"My Tags %@",tags);
			
		}else{
			NSLog(@"FunkyDog:Could not read defaults,will not write either");
		}
	}
	return self;
}
-(void)dealloc{
	[cachesystem release];
	[mydefaults release];
	[super dealloc];
}
-(NSSet*)tags{
	return tags;
}
-(void)updateTags:(NSArray*)tagarray{
	[tags addObjectsFromArray:tagarray];
	NSDictionary *d= [NSDictionary dictionaryWithObject:[tags allObjects] forKey:SGTagsSet];
	[mydefaults setPersistentDomain:d forName: [[NSBundle bundleForClass:[self class]] bundleIdentifier] ];
}
@end